package com.fanhao.dllo.soundfm.activity;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fanhao.dllo.soundfm.R;
import com.fanhao.dllo.soundfm.entity.MainPlayEntity;
import com.fanhao.dllo.soundfm.fragment.MainFragment;
import com.fanhao.dllo.soundfm.service.MusicService;
import com.fanhao.dllo.soundfm.utils.Circlelmage;
import com.fanhao.dllo.soundfm.entity.ServiceToUiString;
import com.fanhao.dllo.soundfm.entity.UiToServiceString;

import de.greenrobot.event.EventBus;

public class MainActivity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout mRelative, relativeLeft;
    private FrameLayout mFrameLayout;
    private TextView titleTv, minTitleTv;
    private ImageView playIv, stopIv, listIv, logoIv, circleIv;
    private Circlelmage mainIv;
    private MusicService musicService;
    private String title, minTitle, url;
    private Bitmap bitmap;
    private MainPlayEntity mainPlayEntity;
    private ObjectAnimator anim;

    private ServiceConnection conn = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicService = ((MusicService.Con) service).getService();
            Log.d("er", "service连接上啦");
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            musicService = null;
        }
    };

    public MusicService getMusicService() {
        return musicService;
    }

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        mRelative = findView(R.id.relative_main);
        mFrameLayout = findView(R.id.frameLayout_main);
        relativeLeft = findView(R.id.relativeLeft_main);
        titleTv = findView(R.id.tv_mainTitle);
        minTitleTv = findView(R.id.tv_mainMinTitle);
        mainIv = findView(R.id.roundedIv_main);
        playIv = findView(R.id.iv_mainPlay);
        stopIv = findView(R.id.iv_mainStop);
        listIv = findView(R.id.iv_mainList);
        logoIv = findView(R.id.iv_logo);
        circleIv = findView(R.id.ivCircle_main);

        playIv.setOnClickListener(this);
        stopIv.setOnClickListener(this);
        relativeLeft.setOnClickListener(this);

        replaceFragment(R.id.frameLayout_main, new MainFragment());

        Intent serviceIntent = new Intent(this, MusicService.class);
        bindService(serviceIntent, conn, BIND_AUTO_CREATE);
        // 订阅事件
        EventBus.getDefault().register(this);


//         给图片设置旋转效果
//      setInterpolator表示设置旋转速率。LinearInterpolator为匀速效果
        anim = ObjectAnimator.ofFloat(mainIv, "rotation", 0, 360);
        LinearInterpolator lin = new LinearInterpolator();
        anim.setInterpolator(lin);
        anim.setDuration(4000);
        anim.setRepeatCount(-1);
        anim.setRepeatMode(ObjectAnimator.RESTART);
    }

    // 接收从playActivity中传来的集合
    public void onEvent(MainPlayEntity entity) {
        title = entity.getTitle();
        minTitle = entity.getAlbum();
        bitmap = entity.getBitmap();
        url = entity.getUrl();
        titleTv.setText(title);
        minTitleTv.setText(minTitle);
        mainIv.setImageBitmap(entity.getBitmap());

        mainPlayEntity = entity;
        circleIv.setVisibility(View.VISIBLE);
        logoIv.setVisibility(View.GONE);

        // 通过判断isTag来得到PlayActivity跳转之前
        // 播放和停止按钮的状态
        if (entity.getIsStop()) {
            // 跳转之前PlayActivity已播放
            playIv.setVisibility(View.VISIBLE);
            stopIv.setVisibility(View.GONE);
            int num = stopIv.getVisibility();
            // 此时 num=8
            if (anim != null) {
                if (num == 8) {
                    // 开始动画
                    pauseAnim();
                }
            }

        } else {
            // 跳转之前PlayActivity正在播放
            playIv.setVisibility(View.GONE);
            stopIv.setVisibility(View.VISIBLE);
            // 此时 num =0
            int num = stopIv.getVisibility();
            if (anim != null) {
                if (num == 0) {
                    // 开始动画
                    startAnim();
                }
            }
        }

    }


    public void onEvent(ServiceToUiString serviceToUiString) {
        switch (serviceToUiString.getType()) {
            case 1:
                int progress = serviceToUiString.getProgress();
                int max = serviceToUiString.getMax();
                Intent intent = new Intent(this, PlayActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("mainPlayEntity", mainPlayEntity.getBitmap());
                intent.putExtras(bundle);
                intent.putExtra("progress", progress);
                intent.putExtra("max", serviceToUiString.getMax());
                intent.putExtra("title", title);
                intent.putExtra("minTitle", minTitle);
                intent.putExtra("url", url);
                startActivity(intent);
                break;
        }


    }

    // 开始动画
    public void startAnim() {
        anim.start();
    }

    // 暂停动画
    public void pauseAnim() {
        // 该属性适用于4.4版本及以上
        anim.pause();
    }

    // 继续动画
    public void resumeAnim() {
        // 该属性适用于4.4版本及以上
        anim.resume();
    }

    // 结束动画
    public void endAnim() {
        anim.end();
    }


    // 隐藏底部音乐播放栏
    public void Gone() {
        mRelative.setVisibility(View.GONE);
    }

    // 再次显示底部音乐播放栏
    public void Visible() {
        mRelative.setVisibility(View.VISIBLE);

    }

    @Override
    public void onClick(View v) {
        PlayActivity playActivity = new PlayActivity();
        switch (v.getId()) {
            case R.id.iv_mainStop:
                stopIv.setVisibility(View.GONE);
                playIv.setVisibility(View.VISIBLE);
                // 用eventBus给musicService发送消息,来停止播放
                EventBus.getDefault().post("stop");
                if (anim != null) {
                    pauseAnim();
                }
                break;
            case R.id.iv_mainPlay:
                playIv.setVisibility(View.GONE);
                stopIv.setVisibility(View.VISIBLE);
                EventBus.getDefault().post(1);
                if (anim != null) {

                    resumeAnim();
                }
                break;
            case R.id.relativeLeft_main:
                // 发送给MusicService一个 type= 4 的UiToServiceString类
                EventBus.getDefault().post(new UiToServiceString("", 4));
                break;
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        musicService = null;
        unbindService(conn);
        EventBus.getDefault().unregister(this);
    }

}
